//
//  TakeTheHallTabBC.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/10/26.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "ServiceTheHallVC.h"
#import "ServiceTheHallHeaderView.h"
#import "ServiceModel.h"
#import "HomeModel.h"
#import "ServiceCell.h"
#import "ServiceContentTabVC.h"
@interface ServiceTheHallVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) ServiceTheHallHeaderView *headerView;
@property(nonatomic,strong) UITableView *tableView;
@property(nonatomic,assign) NSInteger index;
@property(nonatomic,strong) NSMutableArray *dataMuta;
@property(nonatomic,assign) NSInteger page;
@property(nonatomic,assign) NSInteger row;
@property(nonatomic,strong) NSString *urlString;
@property(nonatomic,strong) NSDictionary  *dict;
@property(nonatomic,strong) AFHTTPSessionManager *manager;
@property(nonatomic,strong) NSString *associationtype;
@property(nonatomic,strong) NSString *associationtypesub1;
@property(nonatomic,strong) NSString *associationtypesub2;
@property(nonatomic,strong) NSString *commonindustry;
@property(nonatomic,strong) NSString *industry;
@property(nonatomic,assign) NSInteger type;
@property(nonatomic,assign) NSInteger order;
@end
static NSString *identifierService = @"ServiceCell";
@implementation ServiceTheHallVC
-(NSMutableArray *)dataMuta
{
    if (!_dataMuta) {
        _dataMuta = [NSMutableArray array];
    }
    return _dataMuta;
}
//-(NSMutableArray *)industryMuta
//{
//    if (!_industryMuta) {
//        _industryMuta = [NSMutableArray array];
//    }
//    return _industryMuta;
//}
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.headerView.frame), kVWidth, kVHeight-CGRectGetMaxY(self.headerView.frame)-self.navigationController.navigationBar.frame.size.height-[[UIApplication sharedApplication] statusBarFrame].size.height-kTabBarHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.manager = [AFHTTPSessionManager manager];
    self.headerView = [[ServiceTheHallHeaderView alloc]initWithFrame:CGRectMake(0, 0, kUIScreen.size.width, 100)];
    self.headerView.kNavHeight = kNavAndStatusHight;
    [self.tableView registerNib:[UINib nibWithNibName:@"ServiceCell" bundle:nil] forCellReuseIdentifier:identifierService];
    [self.view addSubview:self.headerView];
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = [UIColor colorWithRed:187/255.0 green:187/255.0 blue:187/255.0 alpha:0.46*0.5];
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    self.tableView.mj_header = header;
    [header beginRefreshing];
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(reloadMore)];
    self.tableView.mj_footer = footer;
                [footer setTitle:[NSBundle bundleLocalizedStringForKey:@"footerTitle"]
 forState:MJRefreshStateNoMoreData];
                footer.stateLabel.font = [UIFont systemFontOfSize:12];
                footer.stateLabel.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1];
    self.associationtype = @"";
    self.associationtypesub1 = @"";
    self.associationtypesub2 = @"";
    self.commonindustry = @"";
    self.industry = @"";
    self.type = 0;
    self.order = 0;
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(associationSearch:) name:@"associationSearch" object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(serviceHallSearchIndustry:) name:@"serviceHallSearchIndustry" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(serviceHallSearchType:) name:@"serviceHallSearchType" object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(serviceHallSearchOrder:) name:@"serviceHallSearchOrder" object:nil];
}
-(void)refresh
{
    self.dataMuta = nil;
    self.page = 1;
    self.row = 10;
    [self reloadInternetData];
    [self.tableView.mj_header endRefreshing];
}
-(void)reloadMore
{
    self.page ++;
    [WSProgressHUD setProgressHUDIndicatorStyle:WSProgressHUDIndicatorBigGray];
    [WSProgressHUD show];
    [self reloadInternetData];

}
-(void)reloadInternet
{
    
    [WSProgressHUD setProgressHUDIndicatorStyle:WSProgressHUDIndicatorBigGray];
    [WSProgressHUD show];
    self.dataMuta = nil;
    self.page = 1;
    self.row = 10;
    [self reloadInternetData];
    [self.tableView.mj_header endRefreshing];
}

-(void)reloadInternetData
{
    NSString *types = [NSString stringWithFormat:@"%ld",self.type];
    NSString *orders = [NSString stringWithFormat:@"%ld",self.order];
    NSString *pages = [NSString stringWithFormat:@"%ld",self.page];
    NSString *rows = [NSString stringWithFormat:@"%ld",self.row];
    self.dict = @{
                  @"uid":@"",
                  @"associationtype":self.associationtype,
                  @"associationtypesub1":self.associationtypesub1,
                  @"associationtypesub2":self.associationtypesub2,
                  @"commonindustry":self.commonindustry,
                  @"industry":self.industry,
                  @"type":types,
                  @"order":orders,
                  @"page":pages,
                  @"rows":rows
                  };
    XLog(@"self.dict:%@",self.dict);
    [CustomeManager POSTWithURLIndex:24 parameters:self.dict success:^(NSDictionary *dict) {
        
    } failure:^(NSError *error) {
        
    }];
}
#pragma mark-通知:行业搜索
-(void)serviceHallSearchIndustry:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    XLog(@"dict:%@",dict);
    self.industry = [dict objectForKey:@"industry"];
    [self reloadInternet];
}
#pragma mark-通知:类型搜索
-(void)serviceHallSearchType:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    XLog(@"dict:%@",dict);
    self.type = [[dict objectForKey:@"type"] integerValue];
    [self reloadInternet];
}

#pragma mark-通知:排序搜索
-(void)serviceHallSearchOrder:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    XLog(@"dict:%@",dict);
    self.order = [[dict objectForKey:@"order"] integerValue];
    [self reloadInternet];
}
-(void)associationSearch:(NSNotification *)info
{
    
    NSDictionary *dict = info.userInfo;
    XLog(@"dict:%@",dict);
    NSArray *date = [dict objectForKey:@"date"];
    if (date.count) {
        for (NSDictionary *dict in date) {
            ServiceModel *model = [ServiceModel mj_objectWithKeyValues:dict];
            [self.dataMuta addObject:model];
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
    }else {
         self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
     }
    [self.tableView reloadData];
    [WSProgressHUD dismiss];
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataMuta.count?self.dataMuta.count:0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
//    ServiceCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierService forIndexPath:indexPath];
    ServiceCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierService forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (self.dataMuta.count) {
        ServiceModel *model = self.dataMuta[indexPath.row];
        cell.serviceModel = model;
    }
    return cell;
   
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 144;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ServiceContentTabVC *homeServiceVC = [[ServiceContentTabVC alloc]init];
    ServiceModel *model = self.dataMuta[indexPath.row];
    homeServiceVC.model = model;
    [self.navigationController pushViewController:homeServiceVC animated:YES];
}
@end
